package com.moco.launcher.fragment;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract.Data;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.moco.launcher.R;
import com.moco.launcher.listener.CloudListener;
import com.oazon.common.util.RequestManager;
import com.oazon.common.util.curcity.AddressCallback;
import com.oazon.common.util.curcity.AddressUtil;
import com.oazon.common.util.curcity.ResponseCityBean;

public class PowerOffFragment extends BaseFragment {

	private static final String TAG = "PowerOffFragment";

	private TextView mTemp = null;
	private ImageView mWeather = null;
	private TextView mShidu = null;
	private TextView mTime = null;
	private TextView mCity = null;
	private TextView textview_weather = null;
	private TextView textview_wendu_zuigao = null;
	private TextView textview_wendu_dangqian = null;
	
	private static String wendu_zuigao_temp = "/28°";
	private static String wendu_dangqian_temp = "21°";
	private static String weather_textview_temp = "晴";
	private static int weather_imageview_id_temp = R.drawable.qing ;

	private String mTempText = null;
	private String mShiduText = null;
	private String mTimeText = null;
	private String mCityText = null;
	private String mWeatherText = null;
	private String mtemperature = null;
	private String mtemperature_zuigao = null;

	private boolean isRunning = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	public void getCity() {
		// TODO 得到当前所在城市
	}

	public void getWeather() {
		AddressUtil.getCurCityName(this.getContext(), new AddressCallback() {

			@Override
			public void onSuccess(String str) {
				try {
					String cityName = "";

					ResponseCityBean bean = JSON.parseObject(str,
							ResponseCityBean.class);
					String cityname = bean.getRetData().getCity();
					if (!"None".equals(cityname)) {
						cityName = cityname;

					} else {
						cityName = bean.getRetData().getProvince();
					}

					String cityNameEncode = "";
					try {
						cityNameEncode = URLEncoder.encode(cityName, "utf-8");
					} catch (UnsupportedEncodingException e1) {
						e1.printStackTrace();
					}
					String url = "http://v.juhe.cn/weather/index?dtype=json&key=35ca3c327c75ed9fbc2ca4a944c81aad&cityname="
							+ cityNameEncode;
					Log.i(TAG, url);
					RequestManager.sendRequest(
							PowerOffFragment.this.getActivity(), url,
							new CloudListener() {
								@Override
								public void onResponse(String response) {
									// TODO Auto-generated method stub
									Log.d(TAG, "response= " + response);
									try {
										parseWeather(response);
										notifyDataChanged();
									} catch (Exception e) {
										Log.d(TAG, "err= " + response);
									}
								}

								@Override
								public void onError(int err, String msg) {
									// TODO Auto-generated method stub
									Log.d(TAG, "err= " + msg);
								}
							});

				} catch (Exception e) {
					onError(e.toString());
				}

			}

			@Override
			public void onError(String er) {

			}
		});
	}

	private void parseWeather(String response) {

		Log.d(TAG, "response= " + response);
		JSONObject weather = JSONObject.parseObject(response);
		String errcode = weather.getString("resultcode");
		if (!errcode.equals("200")) {
			return;
		}

		JSONObject result = weather.getJSONObject("result");
		JSONObject sk = result.getJSONObject("sk");

		mShiduText = sk.getString("humidity");
		mTimeText = sk.getString("time");
		mTempText = sk.getString("temp");
		Log.d(TAG, "shidu=" + mShiduText);
		Log.d(TAG, "time=" + mTimeText);

		JSONObject today = result.getJSONObject("today");

		mWeatherText = today.getString("weather");
		mtemperature = today.getString("temperature");

		String temp = mtemperature;
		String[] mtemperaturearry = temp.split("~");
		temp = mtemperaturearry[1];
		mtemperature_zuigao = temp.substring(0, temp.length() - 1);

		Log.d(TAG, "temp=" + mTempText);
		Log.d(TAG, "weather=" + mWeatherText);
		Log.d(TAG, "mtemperature_zuigao=" + mtemperature_zuigao);
	}

	private void notifyDataChanged() {
		if(mWeatherText!=null){
			textview_weather.setText(mWeatherText);
			weather_textview_temp = mWeatherText;
		}
		if(mTempText!=null){
			textview_wendu_dangqian.setText(mTempText + "°");
			wendu_dangqian_temp = mTempText + "°";
		}
		if(mtemperature_zuigao!=null){
			textview_wendu_zuigao.setText("/" + mtemperature_zuigao + "°");
			wendu_zuigao_temp = "/" + mtemperature_zuigao + "°";
		}
		//del by vincent 15-06-17
		/*		if(mShiduText!=null){
			mShidu.setText(mShiduText);
		}	
		if(mCityText!=null){
			mCity.setText(mCityText);
			mCity.setTextSize(32);
		}	*/
		
		//mTime.setText(getTime());//del by vincent 15-06-04
		
		if (mWeatherText.startsWith("多云")||mWeatherText.equals("多云")||mWeatherText.endsWith("多云")) {
			mWeather.setImageResource(R.drawable.duoyun);
			weather_imageview_id_temp=R.drawable.duoyun;
		} else if (mWeatherText.equals("阵雨")) {
			mWeather.setImageResource(R.drawable.zhenyu);
			weather_imageview_id_temp=R.drawable.zhenyu;
		} else if (mWeatherText.equals("晴")) {
			mWeather.setImageResource(R.drawable.qing);
			weather_imageview_id_temp=R.drawable.qing;
		} else if (mWeatherText.equals("阴")) {
			mWeather.setImageResource(R.drawable.yin);
			weather_imageview_id_temp=R.drawable.yin;
		} else if (mWeatherText.equals("雷阵雨")) {
			mWeather.setImageResource(R.drawable.leiyu);
			weather_imageview_id_temp=R.drawable.leiyu;
		} else if (mWeatherText.equals("雷阵雨伴有冰雹")) {
			mWeather.setImageResource(R.drawable.leiyu);
			weather_imageview_id_temp=R.drawable.leiyu;
		} else if (mWeatherText.equals("雨夹雪")) {
			mWeather.setImageResource(R.drawable.yu);
			weather_imageview_id_temp=R.drawable.yu;
		} else if (mWeatherText.equals("小雨")) {
			mWeather.setImageResource(R.drawable.yu);
			weather_imageview_id_temp=R.drawable.yu;
		} else if (mWeatherText.equals("中雨")) {
			mWeather.setImageResource(R.drawable.yu);
			weather_imageview_id_temp=R.drawable.yu;
		} else if (mWeatherText.equals("大雨")) {
			mWeather.setImageResource(R.drawable.yu);
			weather_imageview_id_temp=R.drawable.yu;
		} else if (mWeatherText.equals("暴雨")) {
			mWeather.setImageResource(R.drawable.yu);
			weather_imageview_id_temp=R.drawable.yu;
		} else if (mWeatherText.equals("大暴雨")) {
			mWeather.setImageResource(R.drawable.yu);
			weather_imageview_id_temp=R.drawable.yu;
		} else if (mWeatherText.equals("特大暴雨")) {
			mWeather.setImageResource(R.drawable.yu);
			weather_imageview_id_temp=R.drawable.yu;
		} else if (mWeatherText.equals("阵雪")) {
			mWeather.setImageResource(R.drawable.xue);
			weather_imageview_id_temp=R.drawable.xue;
		} else if (mWeatherText.equals("小雪")) {
			mWeather.setImageResource(R.drawable.xue);
			weather_imageview_id_temp=R.drawable.xue;
		} else if (mWeatherText.equals("中雪")) {
			mWeather.setImageResource(R.drawable.xue);
			weather_imageview_id_temp=R.drawable.xue;
		} else if (mWeatherText.equals("大雪")) {
			mWeather.setImageResource(R.drawable.xue);
			weather_imageview_id_temp=R.drawable.xue;
		} else if (mWeatherText.equals("暴雪")) {
			mWeather.setImageResource(R.drawable.xue);
			weather_imageview_id_temp=R.drawable.xue;
		} else if (mWeatherText.equals("雾")) {
			mWeather.setImageResource(R.drawable.wu);
			weather_imageview_id_temp=R.drawable.wu;
		} else if (mWeatherText.equals("冻雨")) {
			mWeather.setImageResource(R.drawable.yu);
			weather_imageview_id_temp=R.drawable.yu;
		} else if (mWeatherText.equals("沙尘暴")) {
			mWeather.setImageResource(R.drawable.chen);
			weather_imageview_id_temp=R.drawable.chen;
		} else if (mWeatherText.equals("小雨-中雨")) {
			mWeather.setImageResource(R.drawable.yu);
			weather_imageview_id_temp=R.drawable.yu;
		} else if (mWeatherText.equals("中雨-大雨")) {
			mWeather.setImageResource(R.drawable.yu);
			weather_imageview_id_temp=R.drawable.yu;
		} else if (mWeatherText.equals("大雨-暴雨")) {
			mWeather.setImageResource(R.drawable.yu);
			weather_imageview_id_temp=R.drawable.yu;
		} else if (mWeatherText.equals("暴雨-大暴雨")) {
			mWeather.setImageResource(R.drawable.yu);
			weather_imageview_id_temp=R.drawable.yu;
		} else if (mWeatherText.equals("大暴雨-特大暴雨")) {
			mWeather.setImageResource(R.drawable.yu);
			weather_imageview_id_temp=R.drawable.yu;
		} else if (mWeatherText.equals("小雪-中雪")) {
			mWeather.setImageResource(R.drawable.xue);
			weather_imageview_id_temp=R.drawable.xue;
		} else if (mWeatherText.equals("中雪-大雪")) {
			mWeather.setImageResource(R.drawable.xue);
			weather_imageview_id_temp=R.drawable.xue;
		} else if (mWeatherText.equals("大雪-暴雪")) {
			mWeather.setImageResource(R.drawable.xue);
			weather_imageview_id_temp=R.drawable.xue;
		} else if (mWeatherText.equals("浮尘")) {
			mWeather.setImageResource(R.drawable.chen);
			weather_imageview_id_temp=R.drawable.chen;
		} else if (mWeatherText.equals("扬沙")) {
			mWeather.setImageResource(R.drawable.chen);
			weather_imageview_id_temp=R.drawable.chen;
		} else if (mWeatherText.equals("强沙尘暴")) {
			mWeather.setImageResource(R.drawable.chen);
			weather_imageview_id_temp=R.drawable.chen;
		} else if (mWeatherText.equals("霾")) {
			mWeather.setImageResource(R.drawable.mai);
			weather_imageview_id_temp=R.drawable.mai;
		} else if (mWeatherText.equals("扬沙")) {
			mWeather.setImageResource(R.drawable.chen);
			weather_imageview_id_temp=R.drawable.chen;
		}
		
		Log.i(TAG, "weather_imageview_id_temp=="+weather_imageview_id_temp);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.poweroff_layout, container,
				false);

		textview_weather = (TextView) rootView
				.findViewById(R.id.id_text_weather);
		textview_weather.setText(weather_textview_temp);
		mWeather = (ImageView) rootView.findViewById(R.id.weather);
		mWeather.setImageResource(weather_imageview_id_temp);
		mTime = (TextView) rootView.findViewById(R.id.current_time);

		mTime.setText(getTime());//add by vincent 150604 for clock will disapper sometime
		
		textview_wendu_dangqian = (TextView) rootView
				.findViewById(R.id.id_wendu_dangqian);
		textview_wendu_dangqian.setText(wendu_dangqian_temp);
		textview_wendu_zuigao = (TextView) rootView
				.findViewById(R.id.id_wendu_zuigao);
		textview_wendu_zuigao.setText(wendu_zuigao_temp);
		getWeather();

		if (thread == null) {
			isRunning = true;
			thread = new Thread(runnable);
			thread.start();
		}
		return rootView;
	}

	Handler timeHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			Date date = new Date();
			SimpleDateFormat format = new SimpleDateFormat("HH:mm");
			mTime.setText(format.format(date));
		};
	};

	Thread thread = null;

	private Runnable runnable = new Runnable() {

		@Override
		public void run() {
			while (isRunning) {
				timeHandler.sendEmptyMessage(1);
				try {
					Thread.sleep(10 * 1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	};

	public void onDestroy() {
		isRunning = false;
		if (thread != null) {
			thread.interrupt();
			thread = null;
		}
		super.onDestroy();
	};
	private String getTime (){
			Date date = new Date();
			SimpleDateFormat format = new SimpleDateFormat("HH:mm");
			return format.format(date);
			}
}
